package br.jcrsistemas.componentes;

import java.sql.Timestamp;

import javax.swing.InputVerifier;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import br.jcrsistemas.util.Util;
import br.jcrsistemas.util.ValidDate;

public class DataInputVerifier extends InputVerifier {

    private JTextField textfield;
    private boolean aceitaDataPosteriorAtual;
    private boolean result = true;

    public DataInputVerifier(JTextField textfield) {
        this(textfield, false);
    }

    public DataInputVerifier(JTextField textfield, boolean aceitaDataPosteriorAtual) {
        this.textfield = textfield;
        this.aceitaDataPosteriorAtual = aceitaDataPosteriorAtual;
    }

    @Override
    public boolean verify(JComponent arg0) {

        result = true;

        String data = this.textfield.getText().trim();

        boolean badFormat = false;

        if (data != null && data.length() > 0) {

            if (data.length() == 8) {

                if (!(data.substring(2, 3).equals("/") || data.substring(5, 6).equals("/"))) {

                    data = data.substring(0, 2) + "/" + data.substring(2, 4) + "/" + data.substring(4);
                    this.textfield.setText(data);

                } else {

                    badFormat = true;

                }

            } else if (data.length() == 10) {

                if (!(data.substring(2, 3).equals("/") && data.substring(5, 6).equals("/"))) {

                    badFormat = true;

                }

            } else {

                badFormat = true;

            }

        }

        if (badFormat) {

            JOptionPane.showMessageDialog(null,
                    "Digite a data no formato DD/MM/AAAA", "Erro",
                    JOptionPane.ERROR_MESSAGE);
            result = false;

        } else if (data.length() > 0) {
            if (!ValidDate.isDateValid(data.substring(6, 10), data.substring(3, 5), data.substring(0, 2))) {

                JOptionPane.showMessageDialog(null, "Data inválida.", "Erro",
                        JOptionPane.ERROR_MESSAGE);
                result = false;

            } else if (!aceitaDataPosteriorAtual) {

                try {

                    Timestamp dataDigitada = new Timestamp(Util.formataStringToDate(data).getTime());

                    Timestamp dataAtual = new Timestamp(Util.getDataAtual().getTime());

                    if (dataDigitada.after(dataAtual)) {

                        JOptionPane.showMessageDialog(null,
                                "Data não pode ser superior à data atual.", "Erro",
                                JOptionPane.ERROR_MESSAGE);
                        result = false;

                    }

                } catch (Exception exc) {

                    exc.printStackTrace();

                }

            }

        }

        return result;

    }
}